<?php
/**
 * factory.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2018/09/08
 * Time: 9:49
 */
interface Shape {
    public function draw();
}

class Rectangle implements Shape {
    #Override
    public function draw()
    {
        // TODO: Implement draw() method.
        echo("Inside Rectangle::draw() method.\n");
    }
}
class Square implements Shape {
    #Override
    public function draw()
    {
        // TODO: Implement draw() method.
        echo("Inside Square::draw() method.\n");
    }
}
class Circle implements Shape {
    #Override
    public function draw()
    {
        // TODO: Implement draw() method.
        echo("Inside Circle::draw() method.\n");
    }
}

/**
 * 创建一个工厂，生成基于给定信息的实体类的对象
 */
class ShapeFactory {
    // 使用 getShape 方法获取形状类型的对象
    public function getShape($shapeType='Rectangle'){
        return new $shapeType();
    }
}

// 使用该工厂，通过传递类型信息来获取实体类的对象
$shapeFactory = new ShapeFactory;
$shape1 = $shapeFactory->getShape();
$shape1->draw();
$shape2 = $shapeFactory->getShape('Square');
$shape2->draw();
$shape3 = $shapeFactory->getShape('Circle');
$shape3->draw();